package student

import (
	"XSSS_v2/handler"
	"XSSS_v2/model"
	"XSSS_v2/pkg/errno"
	"github.com/gin-gonic/gin"
	"strconv"
)

// UpdateStu
// @Tags admin
// @Summary 根据id更新学生信息
// @Description Update a student by ID
// @Produce json
// @Param id path uint64 true "student's id"
// @Param user body model.Student true "the student info"
// @Success 200 {object} handler.Response
// @Router /v1/admin/{id} [put]
func UpdateStu(c *gin.Context) {
	//参数 id
	stuId, _ := strconv.Atoi(c.Param("id"))

	//绑定对象
	var s = &model.Student{}
	err := c.Bind(s)
	if err != nil {
		handler.SendResponse(c, errno.ErrBind, nil)
		return
	}
	s.Id = stuId

	//验证数据合法性
	err = s.Validate()
	if err != nil {
		handler.SendResponse(c, errno.ErrValidation, nil)
		return
	}

	//加密密码
	err = s.Encrypt()
	if err != nil {
		handler.SendResponse(c, errno.ErrEncrypt, nil)
		return
	}

	//保存本地
	err = s.UpdateStu()
	if err != nil {
		handler.SendResponse(c, errno.ErrDatabase, nil)
		return
	}
	//响应
	handler.SendResponse(c, nil, "success")
}
